package com.system.core.plugins.mybatis.custom;

import java.util.List;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import lombok.experimental.UtilityClass;

@UtilityClass
public class MyUpdateXML {
	
	public void updateBatchByPrimaryKeyXML(Document document, IntrospectedTable introspectedTable) {
		IntrospectedColumn keyColumns = null;
		if(introspectedTable.getPrimaryKeyColumns().size()>0)
			keyColumns = introspectedTable.getPrimaryKeyColumns().get(0);
		if(keyColumns == null)
			return;
		XmlElement batchUpdateEle = new XmlElement("update");
		batchUpdateEle.addAttribute(new Attribute("id", "updateBatchByPrimaryKey"));
		batchUpdateEle.addAttribute(new Attribute("parameterType", "java.util.List"));
		TextElement zhushi = new TextElement("<!--\r\n"
				+ "      WARNING - @mbg.generated\r\n"
				+ "      This element is automatically generated by MyBatis Generator, do not modify.\r\n"
				+ "    -->");
		XmlElement foreachEle = new XmlElement("foreach");
		foreachEle.addAttribute(new Attribute("collection", "list"));
		foreachEle.addAttribute(new Attribute("item", "item"));
		foreachEle.addAttribute(new Attribute("separator", ";"));
		foreachEle.addAttribute(new Attribute("open", ""));
		foreachEle.addAttribute(new Attribute("close", ""));
		foreachEle.addAttribute(new Attribute("index", "index"));
		TextElement updateTableEle = new TextElement("update "+introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
		XmlElement setEle = new XmlElement("set");
		List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
		for(IntrospectedColumn column:columns) {
			TextElement ifValue = new TextElement(column.getActualColumnName()+" = #{item."+column.getJavaProperty()+",jdbcType="+column.getJdbcTypeName()+"},");
			setEle.addElement(ifValue);
		}
		foreachEle.addElement(updateTableEle);
		foreachEle.addElement(setEle);
		
		StringBuilder wherestr = new StringBuilder("where ");
		wherestr.append(keyColumns.getActualColumnName()+" = #{");
		wherestr.append("item."+keyColumns.getJavaProperty()+",jdbcType=");
		wherestr.append(keyColumns.getJdbcTypeName()+"}");
		TextElement whereEle = new TextElement(wherestr.toString());
		foreachEle.addElement(whereEle);
		batchUpdateEle.addElement(zhushi);
		batchUpdateEle.addElement(foreachEle);
		document.getRootElement().addElement(batchUpdateEle);
	}

	public void updateBatchByPrimaryKeySelectiveXML(Document document, IntrospectedTable introspectedTable) {
		IntrospectedColumn keyColumns = null;
		if(introspectedTable.getPrimaryKeyColumns().size()>0)
			keyColumns = introspectedTable.getPrimaryKeyColumns().get(0);
		if(keyColumns == null)
			return;
		XmlElement batchUpdateEle = new XmlElement("update");
		batchUpdateEle.addAttribute(new Attribute("id", "updateBatchByPrimaryKeySelective"));
		batchUpdateEle.addAttribute(new Attribute("parameterType", "java.util.List"));
		TextElement zhushi = new TextElement("<!--\r\n"
				+ "      WARNING - @mbg.generated\r\n"
				+ "      This element is automatically generated by MyBatis Generator, do not modify.\r\n"
				+ "    -->");
		XmlElement foreachEle = new XmlElement("foreach");
		foreachEle.addAttribute(new Attribute("collection", "list"));
		foreachEle.addAttribute(new Attribute("item", "item"));
		foreachEle.addAttribute(new Attribute("separator", ";"));
		foreachEle.addAttribute(new Attribute("open", ""));
		foreachEle.addAttribute(new Attribute("close", ""));
		foreachEle.addAttribute(new Attribute("index", "index"));
		TextElement updateTableEle = new TextElement("update "+introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
		XmlElement setEle = new XmlElement("set");
		List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
		for(IntrospectedColumn column:columns) {
			XmlElement ifEle = new XmlElement("if");
			ifEle.addAttribute(new Attribute("test", "item."+column.getJavaProperty()+" != null"));
			
			TextElement ifValue = new TextElement(column.getActualColumnName()+" = #{item."+column.getJavaProperty()+",jdbcType="+column.getJdbcTypeName()+"},");
			ifEle.addElement(ifValue);
			setEle.addElement(ifEle);
		}
		foreachEle.addElement(updateTableEle);
		foreachEle.addElement(setEle);
		
		StringBuilder wherestr = new StringBuilder("where ");
		wherestr.append(keyColumns.getActualColumnName()+" = #{");
		wherestr.append("item."+keyColumns.getJavaProperty()+",jdbcType=");
		wherestr.append(keyColumns.getJdbcTypeName()+"}");
		TextElement whereEle = new TextElement(wherestr.toString());
		foreachEle.addElement(whereEle);
		batchUpdateEle.addElement(zhushi);
		batchUpdateEle.addElement(foreachEle);
		document.getRootElement().addElement(batchUpdateEle);
	}
}
